package com.example.wanandroid.controller;

import com.example.wanandroid.pojo.Answer;
import com.example.wanandroid.pojo.Question;
import com.example.wanandroid.pojo.User;
import com.example.wanandroid.result.Result;
import com.example.wanandroid.result.ResultUtil;
import com.example.wanandroid.service.AnswerService;
import com.example.wanandroid.service.QuestionService;
import com.example.wanandroid.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Desc TODO
 * @Date 2020/4/17 21:01
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;
    @Autowired
    UserService userService;
    @Autowired
    AnswerService answerService;


    /**
     * 发布问题
     *
     * @param request
     * @param title
     * @param content
     * @return
     */
    @PostMapping("/addQuestion")
    public Result addQuestion(HttpServletRequest request, String title, String content) {
        Question question = new Question();
        question.setContent(content);
        question.setCreateDate(new Date());
        question.setTitle(title);
        Long userid = (Long) request.getAttribute("userid");
        question.setUserid(userid);
        questionService.insert(question);
        return ResultUtil.success("发布成功");
    }

    @GetMapping("/list/{type}/{pageSize}/{pageNum}")
    public Object listQuestion(HttpServletRequest request, @PathVariable("type") String type, @PathVariable("pageSize") int pageSize, @PathVariable("pageNum") int pageNum) {
        List<Question> list = new ArrayList<>();
        Long userid = (Long) request.getAttribute("userid");
        User user = (User) request.getAttribute("user");
        int start = pageSize * (pageNum - 1);

        if ("all".equals(type)) {
            //所有人的问题
            list = questionService.listAllQuestions(start, pageSize);
        } else if ("my".equals(type)) {
            //我的问题
            list = questionService.listMyQuestions(userid, start, pageSize);
        }

        HashMap<String, Object> result = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<HashMap<String, Object>> listMap = new ArrayList<>();
        for (Question question : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("_id", question.getId());
            String createdAt = sdf.format(question.getCreateDate());
            map.put("createdAt", createdAt);
            map.put("publishedAt", createdAt);
            map.put("desc", question.getContent());
            map.put("title", question.getTitle());
            map.put("who", user.getNickname());
            listMap.add(map);
        }
        result.put("error", false);
        result.put("results", listMap);
        return result;
    }

    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id") long id) {
        Question question = questionService.selectByPrimaryKey(id);
        User user = userService.selectByPrimaryKey(question.getUserid());
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("_id", question.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createdAt = sdf.format(question.getCreateDate());
        resultMap.put("createdAt", createdAt);
        resultMap.put("publishedAt", createdAt);
        resultMap.put("desc", question.getContent());
        resultMap.put("title", question.getTitle());
        resultMap.put("who", user.getNickname());
        return ResultUtil.success(resultMap);
    }

    /**
     * 发布回答
     *
     * @param request
     * @param questionId
     * @param content
     * @return
     */
    @PostMapping("/answer/add")
    public Result addAnswer(HttpServletRequest request, long questionId, String content) {
        Long userid = (Long) request.getAttribute("userid");
        Answer answer = new Answer();
        answer.setUserid(userid);
        answer.setContent(content);
        answer.setCreateDate(new Date());
        answer.setQuestionId(questionId);
        answerService.insert(answer);
        return ResultUtil.success("发布成功");
    }

    /**
     * 查询我的回答列表
     *
     * @param request
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/answer/list/my/{pageSize}/{pageNum}")
    public Result listMyAnswer(HttpServletRequest request, @PathVariable("pageSize") int pageSize, @PathVariable("pageNum") int pageNum) {
        Long userid = (Long) request.getAttribute("userid");
        int start = pageSize * (pageNum - 1);
        List<HashMap<String, Object>> list = answerService.listMyAnswers(userid, start, pageSize);
        return ResultUtil.success(list);
    }


    @GetMapping("/answer/list/{questionId}/{pageSize}/{pageNum}")
    public Result listMyAnswer(HttpServletRequest request, @PathVariable("questionId") long questionId, @PathVariable("pageSize") int pageSize, @PathVariable("pageNum") int pageNum) {
        int start = pageSize * (pageNum - 1);
        List<HashMap<String, Object>> list = answerService.listAnswersByQuestionId(questionId, start, pageSize);
        return ResultUtil.success(list);
    }

}
